package cn.entity.img;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class bigSword extends JPanel {
    private int x;
    private int y;
    private int flySwordX;
    private int flySwordY;
    private BufferedImage[] images;

    public static int flag;
    private int index;
    private int index1;
    private BufferedImage image;

    private int width;
    private int height;
    private boolean isAttacking = false; // 是否正在播放攻击动画
    private int attackFrame = 0; // 攻击动画当前帧
    private long lastFrameTime = 0; // 上一帧时间
    private static final long FRAME_DELAY = 150; // 每帧间隔150毫秒
    private boolean animationFinished = false; // 动画是否播放完成

    public bigSword(int x,int y) throws IOException {
        this.flySwordX = x;
        this.flySwordY = y;
        this.x = x;
        this.y = y;
        width=180;
        height=180;
        flag = 0;
        index = 0;
        index1 = 0;
        images = new BufferedImage[4];

        for(int i = 0; i < images.length; i++) {
            images[i]= ImageIO.read(new File("img/fighter/B"+i+".png"));
        }
        image = images[0];
    }

    public void paintComponent(Graphics g){
        if(isAttacking && !animationFinished) {
            // 播放攻击动画
            updateAttackAnimation();
            g.drawImage(getAttackImage(), x-10, y-65, width, height, null);
        } else {
            // 正常绘制（使用当前蓄力状态对应的图片）
            g.drawImage(getImage(), x, y, 120, 120, null);
        }
    }

    // 开始攻击动画
    public void startAttackAnimation() {
        if(!isAttacking) {
            isAttacking = true;
            animationFinished = false;
            attackFrame = 0; // 从第一帧开始
            lastFrameTime = System.currentTimeMillis();
        }
    }

    // 更新攻击动画帧
    private void updateAttackAnimation() {
        long currentTime = System.currentTimeMillis();
        if(currentTime - lastFrameTime > FRAME_DELAY) {
            attackFrame++;
            lastFrameTime = currentTime;

            // 如果动画播放完成，标记为完成状态
            if(attackFrame >= images.length) {
                animationFinished = true;
                isAttacking = false;
                attackFrame = 0;
            }
        }
    }

    // 获取攻击动画当前帧图片
    public BufferedImage getAttackImage() {
        if(attackFrame < images.length) {
            return images[attackFrame];
        }
        return images[0];
    }

    // 检查是否正在播放攻击动画
    public boolean isAttacking() {
        return isAttacking && !animationFinished;
    }

    // 检查动画是否播放完成
    public boolean isAnimationFinished() {
        return animationFinished;
    }

    // 重置动画状态（用于重新开始）
    public void resetAnimation() {
        isAttacking = false;
        animationFinished = false;
        attackFrame = 0;
    }

    // 强制停止攻击动画（如果需要）
    public void stopAttackAnimation() {
        isAttacking = false;
        animationFinished = true;
        attackFrame = 0;
    }

    // 设置攻击动画位置（与Fighter位置同步）
    public void setAttackPosition(int fighterX, int fighterY) {
        this.x = fighterX;
        this.y = fighterY;
    }

    public void flySwordStep(){
        flySwordX+=30;
    }
    public BufferedImage getImage() {
        return image;
    }

    @Override
    public int getX() {
        return x+30;
    }

    public void setX(int x) {
        this.x = x;
    }

    @Override
    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y-15;
    }
    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }
    public int getIndex() {
        return index;
    }
    public void setIndex(int index) {
        this.index = index;
    }
    public void setIndex1(int index1) {
        this.index1 = index1;
    }
    public int getIndex1(){
        return index1;
    }
    public int getImageLength(){
        return images.length;
    }

    public void setImage(int index){
        this.image = images[index];
    }
}